< previous page page_412 next page >

Page 412
Finally, the ObjectAt function returns the object at a given point. If the point does not lie within the area represented by the quadtree node, the routine returns Nothing. Otherwise, if the node is a leaf node, it uses its objects' IsAt function to find the object at the indicated point. Finally, if the node is not a leaf node, the routine finds the child node that contains the point and invokes its ObjectAt function.
e10a6a0a59f035e870d90cca05b85fce.gif
Option Explicit
' A quadtree node.

' If this is a leaf node, its Objects
' collection contains the objects to draw.
'
' Otherwise the object's children contain other
' QtreeNode objects.

' The maximum number of objects the node can hold.
Private Const MAX_OBJECTS = 100

' The bounds this quadtree node represents.
Public xmin As Single
Public ymin As Single
Public xmid As Single
Public ymid As Single
Public xmax As Single
Public ymax As Single

' The objects, if this is a leaf node.
Private Objects As Collection

' The quadtree children otherwise.
Public NWchild As QtreeNode 
Public NEchild As QtreeNode 
Public SWchild As QtreeNode 
Public SEchild As QtreeNode 

' Add an object to the Objects collection.
'
' If this gives us too many objects, create
' child nodes and subdivide.
Public Sub Add(obj As Object)
    If Objects Is Nothing Then
        ' We are not a leaf node. Put the
        ' object in the appropriate child.
        PlaceObject obj
    Else
        ' We are a leaf node. Add the object
        ' to the Objects collection.
        Objects.Add obj 

        ' See if need to subdivide.

 
< previous page page_412 next page >